<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 
        2. 搜索功能：
          - 收集数据: v-model
          - 绑定事件：v-on
      -->
      <!-- 输入内容直接搜索 -->
      <!-- <input
        type="text"
        placeholder="请输入内容来搜索"
        v-model="searchText"
      /> -->
      <!-- 按回车键搜索 -->
      <input
        type="text"
        placeholder="请输入内容来按回车键搜索"
        @keyup.enter="searchPersons"
      />
      <ul>
        <!-- 1. 遍历展示数据：v-for指令 -->
        <li v-for="person in newPersons" :key="person.id">
          {{ person.name }} - {{ person.age }}
        </li>
      </ul>
      <button>年龄升序</button>
      <button>年龄降序</button>
      <button>原本顺序</button>
    </div>
    <script src="./js/vue.js"></script>
    <script>
      const vm = new Vue({
        data: {
          // 数据
          persons: [
            { id: 1, name: "马冬梅", age: 18 },
            { id: 2, name: "夏洛", age: 30 },
            { id: 3, name: "袁华", age: 25 },
            { id: 4, name: "秋雅", age: 16 },
            { id: 5, name: "大傻春", age: 21 },
            { id: 6, name: "王老师", age: 48 },
            { id: 7, name: "李培华", age: 38 },
            { id: 8, name: "大爷", age: 60 },
          ],
          // 搜索文本
          searchText: "",
        },
        methods: {
          // 搜索触发的函数
          searchPersons(e) {
            this.searchText = e.target.value;
          },
        },
        computed: {
          // 最终要遍历展示的数据
          newPersons() {
            // 计算属性：一旦依赖数据发生变化，就会重新计算
            return this.persons.filter((person) =>
              person.name.includes(this.searchText)
            );
          },
        },
      });
      vm.$mount("#app");
    </script>
  </body>
</html>
